package com.judian.smart.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.judian.smart.R;
import com.judian.smart.holder.HomeViewHolder;
import com.judian.smart.interfaces.OnListItemClickListener;
import com.judian.smart.model.Room;

import java.util.List;

/**
 * author: Peter
 * date: 2019/08/09 星期五 9:34
 * desc:
 */
public class HomeListAdapter extends RecyclerView.Adapter<HomeViewHolder> {
    private Context mContext;
    private List<Room> mData;
    public OnListItemClickListener<Room> mListener = null;
    public int pSelectPosition = 0;

    public HomeListAdapter(Context context, List<Room> data) {
        this.mContext = context;
        this.mData = data;
    }

    @NonNull
    @Override
    public HomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_home, parent, false);
        view.getLayoutParams().height = 448 / 4 - 35;
        return new HomeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeViewHolder holder, final int position) {
        holder.homeName.setText(mData.get(position).getRoomName());
        String homeNameText = (String) holder.homeName.getText();
        if (position == pSelectPosition) {
            holder.homeName.setTextColor(Color.WHITE);
            holder.roomLayout.setBackgroundResource(R.mipmap.room_click);
            switch (homeNameText) {
                case "客厅":
                    holder.roomImage.setBackgroundResource(R.mipmap.sitting_room_not_click);
                    break;
                case "主卧":
                    holder.roomImage.setBackgroundResource(R.mipmap.main_room_not_click);
                    break;
                case "次卧":
                    holder.roomImage.setBackgroundResource(R.mipmap.secend_room_not_click);
                    break;
                case "儿童房":
                    holder.roomImage.setBackgroundResource(R.mipmap.child_room_not_click);
                    break;
                default:
                    holder.roomImage.setBackgroundResource(R.mipmap.sitting_room_not_click);
                    break;
            }
        } else {
            holder.homeName.setTextColor(Color.rgb(131, 186, 255));
            holder.roomLayout.setBackgroundResource(R.mipmap.room_not_click);
            switch (homeNameText) {
                case "客厅":
                    holder.roomImage.setBackgroundResource(R.mipmap.sitting_room_click);
                    break;
                case "主卧":
                    holder.roomImage.setBackgroundResource(R.mipmap.main_room_click);
                    break;
                case "次卧":
                    holder.roomImage.setBackgroundResource(R.mipmap.secend_room_click);
                    break;
                case "儿童房":
                    holder.roomImage.setBackgroundResource(R.mipmap.child_room_click);
                    break;
                default:
                    holder.roomImage.setBackgroundResource(R.mipmap.sitting_room_click);
                    break;
            }
        }

        holder.roomLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.setOnItemClick(mData.get(position), position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}
